package com.second.system.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.second.common.pojo.Permission;
import com.second.common.pojo.RolePermission;
import com.second.system.dao.PermissionDao;
import com.second.system.dao.RolePermissionDao;
import com.second.system.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: second-education-parent
 * @description:
 * @author: 王子朋
 * @create: 2023-05-23 18:32
 **/
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    @Override
    public List<Permission> findByUserId(String userId) {
        List<Permission> permissionList=permissionDao.selectByUserId(userId);
        //1.所有一级菜单
        List<Permission> firsts=new ArrayList<>();
        for(Permission permission:permissionList){
            if(permission.getPid().equals("1")){
                firsts.add(permission);
            }
        }
        //2.查询二级菜单
        for(Permission first:firsts){
            List<Permission> children = findChildren(first.getId(), permissionList);
            first.setChildren(children);
        }
        return firsts;
    }

    @Override
    public List<Permission> findAll() {
        List<Permission> permissionList = permissionDao.selectList(null);
        //1.所有一级菜单
        List<Permission> firsts=new ArrayList<>();
        for(Permission permission:permissionList){
            if(permission.getPid().equals("1")){
                firsts.add(permission);
            }
        }
        //2.查询二级菜单
        for(Permission first:firsts){
            List<Permission> children = findChildren(first.getId(), permissionList);
            first.setChildren(children);
        }
        return firsts;
    }
    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Override
    public List<String> findByRoleId(String roleId) {
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        List<RolePermission> rolePermissions = rolePermissionDao.selectList(wrapper);
        List<String> collect = rolePermissions.stream().map(item -> item.getPermissionId()).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void confirm(String roleId, String[] checkPermissionIds) {
        //删除该角色所具有的权限
        UpdateWrapper<RolePermission> wrapper = new UpdateWrapper<>();
        wrapper.eq("role_id",roleId);
        rolePermissionDao.delete(wrapper);
        //批量添加角色的权限
        List<RolePermission> permissions = Arrays.stream(checkPermissionIds).map(id->new RolePermission(IdUtil.getSnowflakeNextId()+"",roleId,id,0, LocalDateTime.now(),LocalDateTime.now())).collect(Collectors.toList());
        rolePermissionDao.batchInsert(permissions);
    }

    private List<Permission> findChildren(String pid,List<Permission> permissions){
        List<Permission> children=new ArrayList<>();
        for(Permission p:permissions){
            if(p.getPid().equals(pid)){
                children.add(p);
            }
        }
        for(Permission p:children){
            p.setChildren(findChildren(p.getId(),permissions));
        }
        return children;
    }

}
